import errorEvent from '../errorMessage'
const uuid = require('uuid');
const crypto = require("crypto");
const md5 = function (str) {
    var md5 = crypto.createHash("md5");
    var md5Sum = md5.update(str);
    var result = md5Sum.digest("hex");
    return result;
}
export default {
    // async creat(p) {
    //     var id = uuid.v1();
    //     var UT = new Date().getTime();
    //     var CT = new Date().getTime();
    //     var sql = `
    //     INSERT INTO user (service_id, title,       ip,        username,       password,        update_time, create_time) 
    //     VALUES (          '${id}',    '${p.title.replace(/\'/g, "\'")}','${p.ip.replace(/\'/g, "\'")}', '${p.username.replace(/\'/g, "\'")}','${p.password.replace(/\'/g, "\'")}', ${UT},       ${CT});`;
    //     try {
    //         await window.DB.creat(sql);
    //         p.serviceId = id;
    //         p.updateTime = UT;
    //         p.createTime = CT;
    //         return p;
    //     } catch (error) {
    //         errorEvent(error);
    //         return null;
    //     };
    // },
    // async delete(id) {
    //     var sql = `DELETE FROM table_name where service_id == '${id}'`;
    //     try {
    //         await window.DB.delete(sql);
    //         return true;
    //     } catch (error) {
    //         errorEvent(error);
    //         return false;
    //     };
    // },
    // async get() {
    //     try {
    //         var data = await window.DB.find(`SELECT * FROM srevice`);
    //         return data;
    //     } catch (error) {
    //         errorEvent(error);
    //         return [];
    //     };
    // },
    async localProjects() {
        try {
            var data = await window.DB.find(`SELECT * FROM project WHERE disable = 0`);
            return data;
        } catch (error) {
            errorEvent(error);
            return [];
        };
    },
    async deletelocalProject(id) {
        try {
            var Sql = `UPDATE project SET 
            disable = 1
            WHERE project_id = '${id}'`;
            await window.DB.update(Sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async creatLoaclProject(p) {
        try {
            // 项目ID
            var projectId = uuid.v1();
            // 创建时间
            var CT = new Date().getTime();

            // 创建项目
            var defaultProject = `
     INSERT INTO project (project_id, owner_user, title, description, disable, update_time, create_time) 
     VALUES('${projectId}','Local','${p.title.replace(/\'/g, "\'")}', '${p.description.replace(/\'/g, "\'")}', 0, ${CT}, ${CT})`;
            await window.DB.creat(defaultProject);

            // 创建项目人员
            var defaultProjectUser = `
     INSERT INTO project_user (project_user_id, project_id, user_id, update_time, create_time)
     VALUES('${uuid.v1()}','${projectId}','Local',${CT}, ${CT});`;
            await window.DB.creat(defaultProjectUser);

            // 创建项目文件夹
            var defaultProjectFolder = `
     INSERT INTO folder (folder_id,folder_project_id,title,type,description,disable,update_time, create_time)
     VALUES('${uuid.v1()}','${projectId}','根目录',0,'根目录',0,${CT}, ${CT});`;
            await window.DB.creat(defaultProjectFolder);
            p.projectId = projectId;

            return p;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async updateLoaclProject(id, p) {
        try {
            var UT = new Date().getTime();
            var Sql = `UPDATE project SET 
            title = '${p.title.replace(/\'/g, "\'")}',
            description = '${p.description.replace(/\'/g, "\'")}',
            update_time = ${UT}
            WHERE project_id = '${id}'`;
            await window.DB.update(Sql);
            return true;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },

    testServiceVersion(ip, callback, error) {
        $http({
            type: 'get',
            url: 'http://' + ip + '/version',
            success: callback,
            error: error,
            loading: false,
        });
    },

    async GetSeverLink() {
        try {
            var data = await window.DB.find(`SELECT * FROM option WHERE key = 'sever_link'`);
            return data;
        } catch (error) {
            errorEvent(error);
            return [];
        };
    },
    async saveSeverLink(ip, callback) {
        try {
            var data = await window.DB.find(`SELECT * FROM option WHERE key = 'sever_link'`);
            if (data.length) {
                await window.DB.update(`UPDATE option SET value = '${ip}' WHERE key = 'sever_link'`);
            } else {
                await window.DB.creat(`INSERT INTO option (key, value) VALUES('sever_link','${ip}')`);
            };
            callback('success')
        } catch (error) {
            errorEvent(error);
            return [];
        };
    },
    getServeProjects(ip, callback) {
        $http({
            type: 'get',
            url: 'http://' + ip + '/project',
            success: callback,
        });
    },
    deleteSeverProject(id, callback) {
        var userId = store.state.user.userData.userId;
        var ip = store.state.user.ip;
        $http({
            type: 'delete',
            url: 'http://' + ip + '/project',
            data: {
                id: id
            },
            headers: {
                token: userId
            },
            success: callback,
        });
    },
    updateSeverProject(id, data, callback) {
        var userId = store.state.user.userData.userId;
        var ip = store.state.user.ip;
        $http({
            type: 'put',
            url: 'http://' + ip + '/project',
            data: {
                id: id,
                title: data.title,
                description: data.description,
            },
            headers: {
                token: userId
            },
            success: callback,
        });
    },
    creatSeverProject(data, callback) {
        var userId = store.state.user.userData.userId;
        var ip = store.state.user.ip;
        $http({
            type: 'post',
            url: 'http://' + ip + '/project',
            data: data,
            headers: {
                token: userId
            },
            success: callback,
        });
    },
    userLogin(ip, data, callback) {
        $http({
            type: 'post',
            url: 'http://' + ip + '/user/login',
            data: {
                username: data.username,
                password: md5(data.password)
            },
            success: callback,
        });
    },
    getUserProject(callback) {
        var userId = store.state.user.userData.userId;
        var ip = store.state.user.ip;
        $http({
            type: 'get',
            url: 'http://' + ip + '/project/userProject',
            headers: {
                token: userId
            },
            success: callback,
        });

    },
    updateMessage(data, callback) {
        var userId = store.state.user.userData.userId;
        var ip = store.state.user.ip;
        $http({
            type: 'put',
            url: 'http://' + ip + '/user/updateMessage',
            data: data,
            headers: {
                token: userId
            },
            success: callback,
        });
    },

}